package com.wait.onlinepicture.controller;

import com.wait.onlinepicture.annotation.AuthCheck;
import com.wait.onlinepicture.annotation.CommentAuthCheck;
import com.wait.onlinepicture.common.BaseResponse;
import com.wait.onlinepicture.common.DeleteRequest;

import com.wait.onlinepicture.common.ResultUtils;
import com.wait.onlinepicture.exception.BusinessException;
import com.wait.onlinepicture.exception.ErrorCode;
import com.wait.onlinepicture.model.dto.comment.CommentAddRequest;
import com.wait.onlinepicture.model.dto.comment.CommentQueryRequest;
import com.wait.onlinepicture.model.dto.comment.CommentReviewRequest;
import com.wait.onlinepicture.model.entity.PictureComment;
import com.wait.onlinepicture.model.entity.User;
import com.wait.onlinepicture.model.vo.CommentVO;
import com.wait.onlinepicture.service.CommentService;
import com.wait.onlinepicture.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 评论接口
 *
 * @author with葡萄成熟时
 */
@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {

    @Resource
    private CommentService commentService;

    @Resource
    private UserService userService;

    /**
     * 创建评论
     *
     * @param commentAddRequest 评论创建请求
     * @param request HTTP请求
     * @return 评论id
     */
    @PostMapping("/add")
    public BaseResponse<Long> addComment(@RequestBody CommentAddRequest commentAddRequest, HttpServletRequest request) {
        if (commentAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        long result = commentService.addComment(commentAddRequest, loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 删除评论
     *
     * @param deleteRequest 删除请求
     * @param request HTTP请求
     * @return 是否成功
     */
    @PostMapping("/delete")
    @CommentAuthCheck(mustCreator = true)
    public BaseResponse<Boolean> deleteComment(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        long id = deleteRequest.getId();
        boolean result = commentService.deleteComment(id, loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 审核评论
     *
     * @param commentReviewRequest 评论审核请求
     * @param request HTTP请求
     * @return 是否成功
     */
    @PostMapping("/review")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> reviewComment(@RequestBody CommentReviewRequest commentReviewRequest, HttpServletRequest request) {
        if (commentReviewRequest == null || commentReviewRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean result = commentService.reviewComment(commentReviewRequest, loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 根据id获取评论
     *
     * @param id 评论id
     * @return 评论
     */
    @GetMapping("/get")
    public BaseResponse<CommentVO> getCommentById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        PictureComment comment = commentService.getById(id);
        if (comment == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        CommentVO commentVO = commentService.getCommentVO(comment);
        return ResultUtils.success(commentVO);
    }

    /**
     * 分页获取评论列表
     *
     * @param commentQueryRequest 评论查询请求
     * @return 评论列表
     */
    @PostMapping("/list/page")
    public BaseResponse<List<CommentVO>> listCommentByPage(@RequestBody CommentQueryRequest commentQueryRequest) {
        if (commentQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<CommentVO> commentVOList = commentService.listCommentVOByPage(commentQueryRequest);
        return ResultUtils.success(commentVOList);
    }

    /**
     * 获取我的评论列表
     *
     * @param commentQueryRequest 评论查询请求
     * @param request HTTP请求
     * @return 评论列表
     */
    @PostMapping("/my/list/page")
    public BaseResponse<List<CommentVO>> listMyCommentByPage(@RequestBody CommentQueryRequest commentQueryRequest, HttpServletRequest request) {
        if (commentQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        commentQueryRequest.setUserId(loginUser.getId());
        List<CommentVO> commentVOList = commentService.listCommentVOByPage(commentQueryRequest);
        return ResultUtils.success(commentVOList);
    }
}